package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.ExhibitionTicketEntity;
import com.zw.yzk.kabu.data.network.task.ExhibitionTicketTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.mapper.MyTicketMapper;
import com.zw.yzk.kabu.mapper.TicketOrderMapper;
import com.zw.yzk.kabu.view.view.MyTicketView;
import com.zw.yzk.kabu.view.view.TicketOrderView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/9.
 */

public class TicketOrderPresenter implements Presenter {

    private UseCase getTicketList;
    private AppCompatActivity activity;
    private TicketOrderMapper mapper;
    private TicketOrderView ticketOrderView;

    @Inject
    public TicketOrderPresenter(@Named("ticketUseCase") UseCase getTicketList, AppCompatActivity activity, TicketOrderMapper mapper) {
        this.getTicketList = getTicketList;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(TicketOrderView view) {
        this.ticketOrderView = view;
    }

    public void getExhibitionTicketList(int start) {
        getTicketList.execute(new MyTicketSubscriber(activity), new ExhibitionTicketTask(start));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getTicketList.unSubscribe();
    }

    private class MyTicketSubscriber extends BaseSubscriber<ExhibitionTicketEntity> {

        public MyTicketSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(ExhibitionTicketEntity exhibitionTicketEntity) {
            super.onNext(exhibitionTicketEntity);
            ticketOrderView.getTicketSuccess(mapper.transfer(exhibitionTicketEntity.result.ticketList));
        }


        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_exhibition_ticket_failed);
        }
    }
}
